part of '../repository.dart';

const _kRepeatableLearningStageTypeValue = 'repeatable';

class _RepeatableLearningStageDto extends Equatable implements _LearningStageDto {
  const _RepeatableLearningStageDto._internal({
    required this.learningEventDto,
    required this.timeDto,
    required this.repetitionNumber,
  });

  @override
  final _LearningEventDto learningEventDto;

  @override
  final _TimeDto timeDto;

  @override
  final int repetitionNumber;

  @override
  String get type => _kRepeatableLearningStageTypeValue;

  @override
  List<Object?> get props {
    return [
      learningEventDto,
      timeDto,
      repetitionNumber,
    ];
  }

  factory _RepeatableLearningStageDto.fromJson(Map<String, dynamic> json) {
    final timeJson = Map.castFrom<dynamic, dynamic, String, dynamic>(json[_kLearningStageTimeKey]);
    final timeDto = _TimeDto.fromJson(timeJson);

    final learningEventDto = _LearningEventDto.fromString(json[_kLearningStageLearningEventKey]);

    return _RepeatableLearningStageDto._internal(
      learningEventDto: learningEventDto,
      timeDto: timeDto,
      repetitionNumber: json[_kLearningStageRepetitionNumberKey],
    );
  }

  factory _RepeatableLearningStageDto.fromEntity(RepeatableLearningStage repeatableLearningStage) {
    return _RepeatableLearningStageDto._internal(
      learningEventDto: _LearningEventDto.fromEntity(repeatableLearningStage.learningEvent),
      timeDto: _TimeDto.fromEntity(repeatableLearningStage.time),
      repetitionNumber: repeatableLearningStage.repetitionNumber,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      _kLearningStageTypeKey: type,
      _kLearningStageLearningEventKey: learningEventDto.toString(),
      _kLearningStageTimeKey: timeDto.toJson(),
      _kLearningStageRepetitionNumberKey: repetitionNumber,
    };
  }

  @override
  RepeatableLearningStage toEntity() {
    return RepeatableLearningStage(
      learningEvent: learningEventDto.toEntity(),
      time: timeDto.toEntity(),
      repetitionNumber: repetitionNumber,
    );
  }
}
